<?php

	namespace app\payment\behavior;

	use app\base\model\BaseBuyrecordModel;
	use app\wechat\model\MemberWechatModel;
	use app\payment\model\WalletProductModel;
	use app\payment\model\PaymentLogModel;
	use app\payment\model\PaymentOrderModel;
	use think\Cache;
	use think\Db;

	class Payment
	{
		/**
		 * 支付回调
		 * @param $params
		 * @throws \think\Exception
		 */
		public function wxpayCollback($params)
		{
			$data['pay_sn'] = $params['out_trade_no'];
			$data['pay_type'] = 'wxpay';
			$data['trade_no'] = $params['transaction_id'];
			$data['total_fee'] = $params['total_fee'];
			$data['time_end'] = $params['time_end'];
			$data['appid'] = $params['appid'];
			$data['openid'] = $params['openid'];
			PaymentLogModel::SetLog('payment_callback',$params['out_trade_no'], 'Payment支付回调开始');
			//修改支付订单状态为已经支付


			PaymentOrderModel::Status($params['out_trade_no'],
				[
					'pay_type'       => 0,
					'status'         => PaymentOrderModel::ORDER_PAY_PAID,
					'transaction_id' => $params['transaction_id']
				],
				PaymentOrderModel::ORDER_PAY_PAID);

			$BaseBuyrecord = BaseBuyrecordModel::where(['pay_sn' => $params['out_trade_no']])->column('*');
			if ($BaseBuyrecord)
			{
				foreach ($BaseBuyrecord as $item)
				{
					WalletProductModel::Product([
						'member_id'  => $item['member_id'],
						'product_id' => $item['product_id'],
					], $item['num']);
				}
			}


			PaymentLogModel::SetLog('payment_callback', $params['out_trade_no'],'Payment支付回调结束');

		}

		public function alipayCollback($params)
		{

		}
	}